package com.jiaojiao.reggie.utils;

import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * 随机生成验证码工具类
 */
@Component
public class ValidateCodeUtils {
    /**
     * 随机生成验证码
     * @param length 长度为4位或者6位
     * @return
     */
    @CachePut(value = "phoneCode",key = "#phone")
    public String generateValidateCode(int length,String phone){
        Integer code;
        if(length == 4){
            code = new Random().nextInt(9999);//生成随机数，最大为9999
            if(code < 1000){
                code = code + 1000;//保证随机数为4位数字
            }
        }else if(length == 6){
            code = new Random().nextInt(999999);//生成随机数，最大为999999
            if(code < 100000){
                code = code + 100000;//保证随机数为6位数字
            }
        }else{
            throw new RuntimeException("只能生成4位或6位数字验证码");
        }
        return code.toString();
    }

    /**
     * 随机生成指定长度字符串验证码
     * @param length 长度
     * @return
     */
    @CachePut(value = "phoneCode",key = "#phone")
    public String generateValidateCode4String(int length,String phone){
        Random rdm = new Random();
        String hash1 = Integer.toHexString(rdm.nextInt());
        String capstr = hash1.substring(0, length);
        return capstr;
    }


    /**
     * 用来获取redis缓存中电话号码phone所对应的code验证码
     * @param phone
     * @return
     */
    @Cacheable(value = "phoneCode",key = "#phone")
    public String get(String phone) {
        return null;
    }
}